package com.pxmeta.common.core.domain;

import com.google.common.collect.Maps;
import com.pxmeta.common.enums.WebResultEnum;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.HashMap;

/**
 * @description 通用返回类
 * @create 2021-01-27 14:31
 **/
@Data
public class AjaxResult<T> {

    @ApiModelProperty(value = "状态码",example = "200")
    private int code = 200;

    @ApiModelProperty(value = "响应消息",example = "请求成功")
    private String msg = "请求成功";

    @ApiModelProperty(value = "返回数据")
    private T data;


    private static final String defaultMsg = "请求成功";

    @ApiModelProperty(value = "是否成功",example = "true")
    private boolean success = true;
    @ApiModelProperty(value = "总记录数",example = "1")
    private Long total = 0L;
    public static <T>AjaxResult<T> success(T data) {
        return success(data, null);
    }
    public static <T>AjaxResult<T> successPage(T data,Long total) {
        AjaxResult result = new AjaxResult();
        result.setData(data);
        result.setTotal(total);
        return result;
    }

    public static <T>AjaxResult<T> page(T data,Long total) {
        AjaxResult result = new AjaxResult();
        HashMap<String, Object> map = Maps.newHashMap();
        map.put("data",data);
        map.put("total",total);
        result.setData(map);
        result.setTotal(total);
        return result;
    }
    public static <T>AjaxResult <T> success(T data, String msg) {
        AjaxResult<T> result = new AjaxResult<T>();
        result.setData(data);
        result.setMsg(msg);
        return result;
    }

    public static <T>AjaxResult <T> error(int code) {
        return error(code, null);
    }
    public static <T>AjaxResult <T> error() {
        return error(WebResultEnum.ERROR.getStatus(), null);
    }

    public static <T>AjaxResult <T> error(String msg) {
        return error(WebResultEnum.ERROR.getStatus(), msg);
    }


    public static <T> AjaxResult <T> error(WebResultEnum resultEnum){
        return error(resultEnum.getStatus(),resultEnum.getMsg());
    }

    public static <T> AjaxResult <T> error(int code, String msg) {
        AjaxResult<T> result = new AjaxResult<T>();
        result.setSuccess(false);
        result.setCode(code);
        result.setMsg(msg);
        return result;
    }

    public static <T>AjaxResult <T> error(T data, int code, String msg) {
        AjaxResult <T> result = new AjaxResult<T>();
        result.setSuccess(false);
        result.setCode(code);
        result.setMsg(msg);
        result.setData(data);
        return result;
    }

    public static <T> AjaxResult<T> success(String msg) {
        return success(null,  msg);
    }

    public static <T> AjaxResult<T> success() {
        return success(null, defaultMsg);
    }


    public void put(String key, Object value){

    }

}
